package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.dto.ProductInfoVo;
import com.qiche.model.ProductCategory;
import com.qiche.service.ProductCategoryService;
import com.qiche.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品类别管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/categorys",description = "产品类别管理控制器")
@RestController
@RequestMapping("/categorys")
public class ProductCategoryController {
	@Resource
	private ProductCategoryService categoryService;
	@Resource
	private ProductService productService;

	@ApiOperation(value ="根据条件和分页获取产品分类列表" )
	@GetMapping
	public Map<String, Object> getAll(ProductCategory category,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		Map<String, Object> map = new HashMap<>();
		PageInfo<ProductCategory> pageInfo = categoryService.selectByPage(category, page, limit);
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}


	@ApiOperation(value ="获取所有产品分类以及每个分类的产品" )
	@GetMapping(value = "/getAlls")
	public Result getAlls() {
		List<ProductCategory> list = categoryService.selectAll();
		List<ProductInfoVo> vos=new ArrayList<>();
		for (ProductCategory productCategory : list) {
			ProductInfoVo vo=new ProductInfoVo();
			vo.setCategory(productCategory);
			vo.setProducts(productService.selectByCate(productCategory.getId()));
			vos.add(vo);
		}
		return Result.createBySuccess(vos);
	}

	@ApiOperation(value ="获取所有产品分类" )
	@GetMapping(value = "/getAllCategorys")
	public Result getAllCategorys() {
		List<ProductCategory> list = categoryService.selectAll();
		return Result.createBySuccess(list);
	}

	@ApiOperation(value ="添加产品分类" )
	@PostMapping
	public Result add(@RequestBody ProductCategory category) {
		boolean isOk = categoryService.save(category)>0?true:false;
		if(isOk){
			return Result.createBySuccessMessage("添加成功");
		}
		return Result.createByErrorMessage("添加失败");
	}

	@ApiOperation(value ="根据ID获取产品分类信息" )
	@GetMapping(value = "/getById/{id}")
	public Result getById(@PathVariable("id") Long categoryId) {
		ProductCategory category = categoryService.selectById(categoryId);
		return Result.createBySuccess(category);
	}

	@ApiOperation(value ="更新产品分类" )
	@PutMapping
	public Result update(@RequestBody ProductCategory category) {
		int isOk = categoryService.updateNotNull(category);
		if (isOk > 0) {
			return Result.createBySuccessMessage("更新成功");
		}
		return Result.createByErrorMessage("更新失败");
	}

	@ApiOperation(value ="根据ID删除产品分类" )
	@DeleteMapping(value = "/delete/{id}")
	public Result delete(@PathVariable("id") Long id) {
		return 	categoryService.delCategory(id);
	}
}
